package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.service.TeacherService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 教师表的管理
 */
@RestController
@RequestMapping("/tea")
public class TeacherController {
    /**
     * 注入的service，调用业务逻辑的
     */
    @Autowired
    private TeacherService tservice;

    /**
     * 查询全部老师的信息
     * 请求方式为get
     * @return list的集合
     */
    @GetMapping
    public AjaxResult getAll(){
        List<Teacher> list=tservice.getAll();
        return AjaxResult
                .me()
                .setSuccess(true)
                .setMessage("查询成功")
                .setResultObj(list);
    }

    /**
     * 新增老师
     * @param tea
     * @return
     */
    @PutMapping
    public AjaxResult putSaveTeacher(@RequestBody Teacher tea){
        //ID不为空就是修改老师信息
        int i ;
        if(tea.getId()!=null){
            i = tservice.updateByIdSelective(tea);
        }else{
            //ID为空就是添加老师信息
            i = tservice.insert(tea);
        }
        if(i>0) {
            return AjaxResult.me()
                    .setSuccess(true)
                    .setMessage("添加成功")
                    .setResultObj(i);
        }

        return null;
    }

    /**
     * 根据id删除老师信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult DeleteTeacher(@PathVariable("id") Long id){
       //System.out.println(1/0);
        int count = tservice.deleteById(id);

        if(count>0) {
            return AjaxResult.me()
                    .setSuccess(true)
                    .setMessage("删除成功")
                    .setResultObj(count);
        }else{
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }
    /**
     * @Description: 老师信息导出
     * @Author: Neuronet
     * @Date: 2023/9/25 16:49
     * @Return:
     **/
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Teacher> list = tservice.getAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "Teachers", response);
    }

    /**
     * @Description: 老师信息导入模板
     * @Author: Neuronet
     * @Date: 2023/9/25 16:49
     * @param response: 响应对象
     * @Return:
     **/
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "老师信息导入模板", response);
    }
    /**
     * @Description: 老师信息导入
     * @Author: Neuronet
     * @Date: 2023/9/25 17:35
     * @param file: 要导入的文件
     * @Return:
     **/
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        tservice.importExcel(file);
        return AjaxResult.me().setSuccess(true).setMessage("导入成功");
    }
}
